home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / PASCAL.PAS < prev    next >
Pascal/Delphi Source File  |  1985-01-20  |  2KB  |  106 lines

  1. (*
  2.  * pascal
  3.  * creates and draws pascal triangles of any size
  4.  *)
  5.  
  6. program pascal(input, output);
  7.  
  8. var
  9.    ch  : char;    (* used to answer questions *)
  10.  
  11. (*
  12.  * triangle
  13.  * creates the triangle and prints it out.
  14.  * note that only one row is created. To make
  15.  * a node, all you need to know is the value of
  16.  * the node to its left, and the value of the node
  17.  * directly above it. as a result, i only keep the
  18.  * previous row.
  19.  *)
  20.  
  21. procedure triangle;
  22.  
  23. type
  24.    nodeptr = ^node;
  25.    node    = record
  26.       value : integer;    (* value of this node *)
  27.       next  : nodeptr;    (* next node (may be nil) *)
  28.    end;
  29.  
  30. var
  31.    start   : nodeptr;   (* beginning of the last line *)
  32.    curnode : nodeptr;   (* current node *)
  33.    size    : integer;   (* how big the triangle is to be *)
  34.    mult    : integer;   (* the multiples to be removed *)
  35.    last    : integer;   (* value of the last node *)
  36.    heap    : ^integer;  (* pointer to the heap *)
  37.    which   : integer;   (* which node we are printing on the line *)
  38.    left    : integer;   (* how many rows of the triangle left *)
  39.  
  40. (*
  41.  * print
  42.  * prints out the current node
  43.  *)
  44.  
  45. procedure print(value: integer);
  46.  
  47. begin
  48.    if mult = 0 then     (* if mult = 0 => user wants real triangle *)
  49.       write(value:4)
  50.    else
  51.       if (value mod mult) = 0 then
  52.          write('  ')
  53.       else
  54.          write('* ')
  55. end;
  56.  
  57. begin
  58.    mark(heap);        (* used to free up space *)
  59.  
  60.    write('Which multiples do you want printed as spaces? ');
  61.    readln(mult);
  62.    write('Size of the triangle? ');
  63.    readln(size);
  64.  
  65. (*
  66.  * create the first row.
  67.  * note that it is being created from
  68.  * the left side toward the right.
  69.  *)
  70.  
  71.    for which := 1 to size do
  72.    begin
  73.       print(1);
  74.       new(curnode);
  75.       curnode^.value := 1;
  76.       curnode^.next := start;
  77.       start := curnode
  78.    end;
  79.    writeln;
  80.  
  81.    for left := 1 to size do
  82.    begin
  83.       last := 0;
  84.       curnode := start;
  85.       for which := 1 to (size - left) do
  86.       begin
  87.          print(last+curnode^.value);
  88.          curnode^.value := curnode^.value + last;
  89.          last := curnode^.value;
  90.          curnode := curnode^.next
  91.       end;
  92.       writeln
  93.    end;
  94.    release(heap)
  95. end;
  96.  
  97. begin
  98.    repeat
  99.       triangle;
  100.       writeln;
  101.       write('Continue? ');
  102.       readln(ch)
  103.    until (ch <> 'y') and (ch <> 'Y')
  104.  
  105. end.
  106.